var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
var DEFAULT_PATTERN = 'yyyy-MM-dd';
function padding(s, len) {
    var len = len - (s + '').length;
    for (var i = 0; i < len; i++) { s = '0' + s; }
    return s;
};
let date = {
  getQueryStringByName: function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    var context = "";
    if (r != null)
      context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
  },
  formatDate: {
    format: function (date, pattern) {
      pattern = pattern || DEFAULT_PATTERN;
      return pattern.replace(SIGN_REGEXP, function ($0) {
        switch ($0.charAt(0)) {
          case 'y': return padding(date.getFullYear(), $0.length);
          case 'M': return padding(date.getMonth() + 1, $0.length);
          case 'd': return padding(date.getDate(), $0.length);
          case 'w': return date.getDay() + 1;
          case 'h': return padding(date.getHours(), $0.length);
          case 'm': return padding(date.getMinutes(), $0.length);
          case 's': return padding(date.getSeconds(), $0.length);
        }
      });
    },
    parse: function (dateString, pattern) {
      var matchs1 = pattern.match(SIGN_REGEXP);
      var matchs2 = dateString.match(/(\d)+/g);
      if (matchs1.length == matchs2.length) {
        var _date = new Date(1970, 0, 1);
        for (var i = 0; i < matchs1.length; i++) {
          var _int = parseInt(matchs2[i]);
          var sign = matchs1[i];
          switch (sign.charAt(0)) {
            case 'y': _date.setFullYear(_int); break;
            case 'M': _date.setMonth(_int - 1); break;
            case 'd': _date.setDate(_int); break;
            case 'h': _date.setHours(_int); break;
            case 'm': _date.setMinutes(_int); break;
            case 's': _date.setSeconds(_int); break;
          }
        }
        return _date;
      }
      return null;
    },
    changeMoneyToChinese:function (money){
      var cnNums = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖"); //汉字的数字
      var cnIntRadice = new Array("","拾","佰","仟"); //基本单位
      var cnIntUnits = new Array("","万","亿","兆"); //对应整数部分扩展单位
      var cnDecUnits = new Array("角","分","毫","厘"); //对应小数部分单位
      //var cnInteger = "整"; //整数金额时后面跟的字符
      var cnIntLast = "元"; //整型完以后的单位
      var maxNum = 999999999999999.9999; //最大处理的数字

      var IntegerNum; //金额整数部分
      var DecimalNum; //金额小数部分
      var ChineseStr=""; //输出的中文金额字符串
      var parts; //分离金额后用的数组，预定义
      var zeroCount,IntLen;
      if( money == "" ){
        return "";
      }
      money = parseFloat(money);
      if( money >= maxNum ){
        $.alert('超出最大处理数字');
        return "";
      }
      if( money == 0 ){
        //ChineseStr = cnNums[0]+cnIntLast+cnInteger;
        ChineseStr = cnNums[0]+cnIntLast
        //document.getElementById("show").value=ChineseStr;
        return ChineseStr;
      }
      money = money.toString(); //转换为字符串
      if( money.indexOf(".") == -1 ){
        IntegerNum = money;
        DecimalNum = '';
      }else{
        parts = money.split(".");
        IntegerNum = parts[0];
        DecimalNum = parts[1].substr(0,4);
      }
      if( parseInt(IntegerNum,10) > 0 ){//获取整型部分转换
        zeroCount = 0;
        IntLen = IntegerNum.length;
        for(var i=0;i<IntLen;i++ ){
          var n = IntegerNum.substr(i,1);
          var p = IntLen - i - 1;
          var q = p / 4;
          var m = p % 4;
          if( n == "0" ){
            zeroCount++;
          }else{
            if( zeroCount > 0 ){
              ChineseStr += cnNums[0];
            }
            zeroCount = 0; //归零
            ChineseStr += cnNums[parseInt(n)]+cnIntRadice[m];
          }
          if( m==0 && zeroCount<4 ){
            ChineseStr += cnIntUnits[q];
          }
        }
        ChineseStr += cnIntLast;
        //整型部分处理完毕
      }
      if( DecimalNum!= '' ){//小数部分
        let decLen = DecimalNum.length;
        for( i=0; i<decLen; i++ ){
          n = DecimalNum.substr(i,1);
          if( n != '0' ){
            ChineseStr += cnNums[Number(n)]+cnDecUnits[i];
          }
        }
      }
      if( ChineseStr == '' ){
        //ChineseStr += cnNums[0]+cnIntLast+cnInteger;
        ChineseStr += cnNums[0]+cnIntLast;
      }/* else if( DecimalNum == '' ){
         ChineseStr += cnInteger;
         ChineseStr += cnInteger;
         } */
      return ChineseStr;
    },
    parseDate:function (ms) {
      if((ms.getMonth() + 1)<10){
        return ms.getFullYear() + "-0" + (ms.getMonth() + 1) + "-" + ms.getDate();
      }else {
        return ms.getFullYear() + "-" + (ms.getMonth() + 1) + "-" + ms.getDate();
      }

    }

  },
  //在原有的日期上增加天数，默认增加一天
  addCurrentDate: function(days = 1,formStr) {
    let date = new Date();
    date.setDate(date.getDate() + days);
    if(formStr){
      return this.formatDate.format(date,formStr)
    }else {
      return date;
    }
  },

  addDate: function(dateString,days = 1,formStr) {
    let date = new Date(dateString);
    date.setDate(date.getDate() + days);
    if(formStr){
      return this.formatDate.format(date,formStr)
    }else {
      return date;
    }
  },

  objectToFormData: function(obj, form, namespace) {
    var fd = form || new FormData();
    var formKey;
    if(obj instanceof Array){
      for(var item of obj ){
        if(typeof item === 'object' && !(item instanceof File)){
          this.objectToFormData(item, fd, namespace+'[]');
        }else{
          // 若是数组则在关键字后面加上[]
          fd.append(namespace+'[]',item)
        }

      }
    }else{
      for(var property in obj) {
        if(obj.hasOwnProperty(property) && obj[property]) {

          if(namespace) {
            // 若是对象，则这样
            formKey = namespace + '[' + property + ']';
          } else {
            formKey = property;
          }

          // if the property is an object, but not a File,
          // use recursivity.
          if(typeof obj[property] === 'object' && !(obj[property] instanceof File)) {
            // 此处将formKey递归下去很重要，因为数据结构会出现嵌套的情况
            this.objectToFormData(obj[property], fd, formKey);
          } else {

            // if it's a string or a File object
            fd.append(formKey, obj[property]);
          }

        }
      }
    }
    return fd;

  }


};
export default date
